package cn.johnyu.service.impl;

import cn.johnyu.service.AuthenPersistenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@Primary
public class AuthenPersistenceServiceRedisImpl implements AuthenPersistenceService {
    @Autowired
    private ReactiveRedisTemplate<String, String> reactiveRedisTemplate;
    @Override
    public Mono<Boolean> save(String token) {
        return reactiveRedisTemplate.opsForValue().set(token,"");
    }

    @Override
    public Mono<Boolean> check(String token) {
        if(token==null){
            return Mono.just(false);
        }
        return reactiveRedisTemplate.opsForValue()
                .get(token)
                .map(s->true)
                .defaultIfEmpty(false);
    }

    @Override
    public Mono<Boolean> delete(String token) {
        return reactiveRedisTemplate.opsForValue().delete(token);
    }
}
